package com.imd.s9.controller;

import com.alibaba.fastjson.JSONObject;
import com.imd.base.BaseControl;
import com.imd.base.BusinessException;
import com.imd.s9.entity.CustomActionParam;
import com.imd.s9.service.ICustomService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cuiyu
 * @description
 * @date 2024/7/15 11:48:55
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/action")
public class ActionController extends BaseControl {
    @Resource
    private ICustomService customService;

    @PostMapping("/button")
    public void customAction(HttpServletResponse response, @RequestBody CustomActionParam param) throws BusinessException {
        param.setTenantId(getTenantId());
        param.setUserVO(getApiUser());
        Object data = customService.customAction(response, param);
        if (data instanceof Boolean) {
            if (!(Boolean) data) sendResponse();
        } else sendResponse(data);
    }
    @PostMapping("/button/file")
    public void customActionWithFile(HttpServletResponse response, @RequestPart MultipartFile file,
                                     @RequestParam(value = "contentParam") String contentParam) throws BusinessException {
        if (contentParam.endsWith(",")) {
            contentParam = contentParam.substring(0, contentParam.length() - 1);
        }
        CustomActionParam param = JSONObject.parseObject(contentParam, CustomActionParam.class);
        param.setTenantId(getTenantId());
        param.setUserVO(getApiUser());
        param.setFile(file);
        Object data = customService.customAction(response, param);
        if (data instanceof Boolean) {
            if (!(Boolean) data) sendResponse();
        } else sendResponse(data);
    }
    @PostMapping("/param")
    public void customOp(HttpServletResponse response, @RequestBody CustomActionParam param) throws BusinessException {
        customService.customOp(response,param);
    }
}
